<template>
  <Layout>

    <Form ref="form1" :model="form1" :label-width="120">
      <Row class="query-form" style="padding-top: 15px">
        <Col span="8">
          <FormItem label="企业名称：" prop="order">
            <cascader-dom class="query-form-el" ref='queryOrgCascader' @setOrgInfo='setQueryCompanyName' :orgTree="true"
                          :setCurrentName="{label:form1.companyName,value:form1.companyId}"></cascader-dom>
          </FormItem>
        </Col>
        <Col span="8">

        </Col>
        <Col span="8">
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
      </Row>
    </Form>

    <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据！" :loading="itemloading" size="small"
           border :columns="columns1" :data="dataTable">
    </Table>

    <Row style="margin-right:10px;">
      <Col span="24">
        <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange"
              @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer
              show-elevator/>
      </Col>
    </Row>
  </layout>
</template>

<script>
import {Api} from '@/api/index.js'
import {sessionStorage} from '@/common/cacheUtils.js';
import {formatDate} from "@/tools";

var cascaderDom = () => import('@/components/cascader-dom.vue');
export default {
  components: {cascaderDom},
  props: {
    type: {
      type: String,
      required: true
    },
  },
  data() {
    return {
      itemloading: false,
      createUserId: '',
      form1: {},
      form2: {
        members: '',
        membersId: ''
      },
      //分页参数
      pageSizeArray: [10, 20, 50],
      pagedata: {
        total: 0,
        pageSize: 10,
        currentPage: 1,
      },

      columns1: [
        {
          title: "序号",
          width: 80,
          align: "center",
          fixed: 'left',
          render: (h, params) => {
            return h('span', params.index + (this.pagedata.currentPage - 1) * this.pagedata.pageSize + 1);
          }
        },
        {key: 'planName', title: '应急计划名称', align: 'center', width: 200},
        {key: 'measureName', title: '应急预案名称', align: 'center', width: 200},
        {key: 'companyName', title: '所属单位', align: 'center', minWidth: 140},
        {key: 'twoLevelOrgName', title: '所属二级集团', align: 'center', width: 160},
        {
          key: 'startDate', title: '起止时间', align: 'center', minWidth: 200, render: (h, params) => {
            return h('span', this.formatePlanDate(params.row.startDate, params.row.endDate));
          }
        },
        {key: 'createUserName', title: '填报人', align: 'center', minWidth: 100},
        {key: 'phone', title: '填报人电话', align: 'center', minWidth: 100},
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          minWidth: 100,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('A', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  fontSize: '12px',
                },
                on: {
                  click: () => {
                    this.save(params.row);
                  }
                }
              }, '选择'),
            ]);
          }
        }
      ],
      buttonLoading: false, tableHeight: 450,
      dataTable: [],
      codingSortList: [],
      emLevelList: [],
      eventTypeList: [],
      emTypeList: [],
    }
  },
  mounted: function () {
    this.tableHeight = window.innerHeight - 420;
    this.createUserId = JSON.parse(sessionStorage('paramUserInfo').get('userInfo'))["createUserId"]
    //this.selectCodingSort();
    this.query();
  },
  methods: {
    pageChange(pageNum) {
      this.pagedata.currentPage = pageNum;
      this.query();
    },
    formatePlanDate(startDate, endDate) {
      if (startDate && endDate) {
        return formatDate(new Date(startDate), 'yyyy-MM-dd') + "~" + formatDate(new Date(endDate), 'yyyy-MM-dd')
      } else {
        return "--";
      }

    },
    pageSizeChange(pageSize) {
      this.pagedata.pageSize = pageSize;
      this.query();
    },

    copyArray(arr) {
      return JSON.parse(JSON.stringify(arr));
    },
    selectCodingSort() {
      this.$http.get(Api.queryCodeDetailBySortId + '/PL-000001', {params: {}}).then((res) => {
        this.emLevelList = res.data.data;
        this.codingSortList = this.codingSortList.concat(this.emLevelList);
      });
      this.$http.get(Api.queryCodeDetailBySortId + '/ET-000001', {params: {}}).then((res) => {
        this.eventTypeList = res.data.data;
        this.codingSortList = this.codingSortList.concat(this.eventTypeList);
      });
      this.$http.get(Api.queryCodeDetailBySortId + '/PL-000010', {params: {}}).then((res) => {
        this.emTypeList = res.data.data;
        this.codingSortList = this.codingSortList.concat(this.emTypeList);
      });
    },
    getCodingSortName(codingCodeId) {
      let codingName = ''
      try {
        this.codingSortList.forEach(function (e) {
          if (codingCodeId == e.codingCodeId) {
            codingName = e.codingName
            throw Error();
          }
        });
      } catch (e) {
      }
      return codingName;
    },

    save(row) {
      this.$emit('selectMission', row);

    },

    closeModal() {
      this.$emit('closeModal1');
    },

    query() {
      this.itemloading = true;
      this.form1.type = this.type;
      let data = {
        startPage: this.pagedata.currentPage,
        size: this.pagedata.pageSize,
        conditions: this.form1,
      };
      data = Object.assign(data, JSON.parse(JSON.stringify(this.form1)));


      let url = Api.queryEmergencyPlan
      this.$http.post(url, data).then((res) => {
        if (res.data.status === 201) {
          this.$Message.error(res.data.msg);
        }
        this.dataTable = res.data.data.data;
        this.pagedata.total = res.data.data.totalSize;
        this.itemloading = false;
      }).catch(e => {
        this.itemloading = false;
        this.$Message.error("数据获取失败")
      })
    },
    formatDate(date, fmt) {
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
      }
      let o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      };
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + '';
          fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : this.padLeftZero(str));
        }
      }
      return fmt;
    },
    padLeftZero(str) {
      return ('00' + str).substr(str.length);
    },

    reset() {
      this.form1 = {};
      if (this.$refs.queryOrgCascader) {
        this.$refs.queryOrgCascader.resetCurrentOrg();
      }
      if (this.$refs.queryOrgCascader1) {
        this.$refs.queryOrgCascader1.resetCurrentOrg();
      }
      this.pagedata.currentPage = 1
      this.query()
    },
    setQueryCompanyName(data) {
      this.$set(this.form1, 'companyId', data[data.length - 1])
    },
  }

}
</script>
